package tyk.pojo;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/***
 *@title Cart
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/17 22:24
 **/
public class Cart {
    private Map<Integer,CartItems> map;
    public Cart() {
        map=new HashMap<>();
    }

    public Integer getCountTocal() {
        Integer countTocal= 0;
        for(Object obj : map.keySet()) {
            Integer key = (Integer) obj;//取到每一个key值
            CartItems cartItems = map.get(key);
            countTocal+=cartItems.getCount();
        }
        return countTocal;
    }

    public BigDecimal getPriceTocal() {
        BigDecimal priceTocal= BigDecimal.valueOf(0);
        for(Object obj : map.keySet()) {
            Integer key = (Integer) obj;//取到每一个key值
            CartItems cartItems = map.get(key);
            priceTocal = priceTocal.add(cartItems.getPriceTocal());
        }
        return priceTocal;
    }
    public void add (Book book){
        if(map.get(book.getId())==null){
            map.put(book.getId(),new CartItems(book.getId(),book.getName(),1,book.getPrice(),book.getPrice(),0,null,book.getMerchant_id()));
        }else{
            map.get(book.getId()).setCount(map.get(book.getId()).getCount()+1);
            map.get(book.getId()).setPriceTocal(new BigDecimal(String.valueOf(map.get(book.getId()).getPriceTocal().add(book.getPrice()))));
        }
    }
    public void DeleteById(Integer id){
        map.remove(id);
    }
    public void Clear(){
        map.clear();
    }
    public Map<Integer, CartItems> getMap() {
        return map;
    }

    public void setMap(Map<Integer, CartItems> map) {
        this.map = map;
    }

    public void Update(Integer id,Integer count){
        map.get(id).setCount(count);
        map.get(id).setPriceTocal(map.get(id).getPrice().multiply(new BigDecimal(map.get(id).getCount())));
    }
    @Override
    public String toString() {
        return "Cart{" +
                "countTocal=" + getCountTocal() +
                ", priceTocal=" + getPriceTocal() +
                ", map=" + map +
                '}';
    }
}
